package com.smart.manager.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.common.exception.SmartException;
import com.smart.manager.system.entities.body.RSystemRole;
import com.smart.manager.system.entities.enums.AccountType;
import com.smart.manager.system.entities.models.SystemRole;
import com.smart.manager.system.entities.models.query.QSystemRole;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Log4j2
@RequiredArgsConstructor
public class RoleRepository {


    public Page<SystemRole> pageList(RSystemRole.SearchBody search) {
        QSystemRole query = new QSystemRole();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));

        query.order().supperRole.desc().order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<SystemRole>().load(query.findPagedList());
    }

    public void add(RSystemRole.EditBody body) {
        QSystemRole query = new QSystemRole();
        SystemRole record = query.code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:, 角色代码已存在");
        SystemRole systemRole = BeanUtil.copyProperties(body, SystemRole.class);
        systemRole.setSupperRole(false).save();
    }


    public void update(Long id, RSystemRole.EditBody body) {
        QSystemRole query = new QSystemRole();
        SystemRole record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<SystemRole> list = new QSystemRole().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (SystemRole item : list) {
            if (item.getSupperRole()) {
                throw new SmartException("禁止删除超级管理员");
            }

        }
        return DB.deleteAll(list);


    }


    /**
     * 获取数据字典下拉选项
     *
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions(String type) {
        QSystemRole qSystemRole = new QSystemRole();

        Opt.ofBlankAble(type).ifPresent(x -> qSystemRole.accountType.eq(AccountType.valueOf(type)));
        return
                qSystemRole.findStream()
                        .map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getCode()).setDisabled(false))
                        .collect(Collectors.toList());
    }


}
